package com.kai.demo.controller;

import com.kai.demo.consts.RedisKey;
import com.kai.demo.entity.User;
import com.kai.demo.service.UserService;
import com.kai.demo.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>Project: sprigncloud-demo </p>
 * <p>Description: </p>
 * <p>Copyright (c) 2024 Karrytech (Shanghai/Beijing) Co., Ltd.</p>
 * <p>All Rights Reserved.</p>
 *
 * @author <a href="mailto:zhangkai@karrytech.com">Zhang Kai</a>
 */
@RestController
@RequestMapping("/kai/redis/ranking")
public class RankingController {

    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private UserService userService;

    @PostMapping("/update")
    public void update(@RequestParam BigInteger id, @RequestParam Double score) {
        redisUtil.zSet(RedisKey.RANKING + ":" + id, id, score);
    }

    @PostMapping("/get")
    public Set<User> get(@RequestParam BigInteger id, @RequestParam Long start, @RequestParam long end) {
        Set<Object> idSet = redisUtil.reverseRange(RedisKey.RANKING + ":" + id, start, end);
        Set<User> users = new HashSet<>();
        for (Object idIndex : idSet) {
            users.add(userService.get((Integer) idIndex));
        }
        return users;
    }
}